var _this = this;

import Taro from '@tarojs/taro-rn';
import React from 'react';

/**
 * 系统公共数据
 */
export const companyName = '西部硅谷';
export const servicePhone = '18987422570';
export const host = 'http://ynjcjx.com';
export const uploadPath = '/api/services/app/configuration/Upload';
/**
 * 当前系统对应公共数据
 */
/**
 * 系统公共方法
 */
export const showToast = title => {
  Taro.showToast({ title, image: 'none' });
};
export const showModel = (title, content, confirmText = '确定', callBack, errorBack) => {
  Taro.showModal({
    title,
    content,
    confirmText
  }).then(res => {
    if (res.confirm) {
      callBack;
    } else {
      errorBack && errorBack;
    }
  });
};
export const setUserInfo = info => {
  Taro.setStorageSync("user", info ? JSON.stringify(info) : "");
};
// 获取用户信息
export const getUserInfo = () => {
  var data = Taro.getStorageSync("user") || {};
  return Object.keys(data).length > 0 ? JSON.parse(data) : '';
};
// 获取token
export const getToken = () => {
  const user = getUserInfo() || {};
  if (Object.keys(user).length <= 0) return '';
  return { Authorization: "Bearer " + user.token };
  //return { Authorization: "Bearer HDZ1zkJ9xyj_DjH-VdDATOkdF5XlzBRiBNr7x3T0uuqBd8QPYaKvPFGKgQ5bJaU9Bhw5Qde4fSbJANr3Oddme5bS2C-KVa3irOxOWyWgHPZt6ZTUDJKqkJG3xHPIhGiPi3fnwV-HzZDc5ySU0svm97k6cQf3eBpVlJILkQrF9ftxNeZ7PBHAAFtNno8R-Nls3rnyLjJHun1FuZnCWlQkKzsHT2a7qgU-o5xXBPcS7gzuPzE2A30HvWvCBWeRAk9g7kODhb2-L6hG-Q8UwdyDf5w1cBqwyIKmThE0kaC2D91ERp6TImz1ULZleS6wOtp8O6Za8dQF9tEfELn-yT5xE6sPWLrb4ya-vfUgwDD3H6OCHZbpKOKt985kOLFfNApBOjyNNG8eqcvZqt7eWq4a4YZ-z1qK9FNrkq_dtS4a_vsakeT-hqxH3EdIWy0bu2q_4vr97mM7FbizEALR8K0LpTEMlqrM1Pjkfm0ms95djYBvUiS4weBtC903-X94_4Oz" };
};
export const navigateTo = (url, isLogin = false) => {
  if (isLogin) {
    Taro.navigateTo({ url: '/pages/login/login' });
    return;
  }
  Taro.navigateTo({ url });
};
export const navigateBack = (delta = 1) => {
  Taro.navigateBack({ delta });
};
export const redirectTo = url => {
  Taro.redirectTo({ url });
};
export const wxPay = (payParams, callback) => {
  Taro.requestPayment({
    timeStamp: payParams.timeStamp,
    nonceStr: payParams.nonceStr,
    package: payParams.package,
    signType: payParams.signType,
    paySign: payParams.paySign
  }).then(res => {
    callback(res);
  }).catch(res => {
    console.log(res);showToast('支付失败');
  });
};
export const wxH5Pay = (payParams, callback) => {
  // alert(JSON.stringify(payParams));
  if (typeof window['WeixinJSBridge'] == "undefined") {
    if (document.addEventListener) {
      document.addEventListener('WeixinJSBridgeReady', () => onBridgeReady(payParams, callback), false);
    } else if (document['attachEvent']) {
      document['attachEvent']('WeixinJSBridgeReady', () => onBridgeReady(payParams, callback));
      document['attachEvent']('onWeixinJSBridgeReady', () => onBridgeReady(payParams, callback));
    }
  } else {
    _this.onBridgeReady(payParams, callback);
  }
};
/**
 *  微信支付
 */
const onBridgeReady = (payParams, callback) => {
  const that = _this;
  window['WeixinJSBridge'].invoke('getBrandWCPayRequest', payParams, function (res) {
    if (res.err_msg == "get_brand_wcpay_request:ok") {
      callback();
    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
      that.showToast("支付取消");
    } else if (res.err_msg == "get_brand_wcpay_request:fail") {
      that.showToast('支付失败');
    }
  });
};